package cn.stronglink.asset.module.assetmanage.using.service;

import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.IdWorker;

import cn.stronglink.asset.model.AssetExtUsing;
import cn.stronglink.asset.model.AssetRecord;
import cn.stronglink.asset.model.ItAsset;
import cn.stronglink.asset.model.UsingApply;
import cn.stronglink.asset.model.UsingApplyDetail;
import cn.stronglink.asset.module.assetRecord.mapper.AssetRecordMapper;
import cn.stronglink.asset.module.assetmanage.itasset.mapper.ItassetMapper;
import cn.stronglink.asset.module.assetmanage.using.mapper.AssetExtUsingMapper;
import cn.stronglink.asset.module.assetmanage.using.mapper.UsingMapper;
import cn.stronglink.suite.core.util.WebUtil;

@Service
@Transactional(rollbackFor=Exception.class) 
public class UsingService {

	@Autowired
	private UsingMapper usingMapper;
	@Autowired
	private AssetExtUsingMapper assetExtUsingMapper;
	@Autowired
	private ItassetMapper itassetMapper;
	@Autowired
	private AssetRecordMapper assetRecordMapper;
	

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page<UsingApplyDetail> queryList(Page<UsingApplyDetail> page, Map<String, Object> params) {
		page.setRecords(usingMapper.queryList(page,params));
		return page;	
	}

	public void remove(Long id) {
		usingMapper.remove(id);
		
	}

	public void edit(UsingApplyDetail detail) {
		if (detail.getId() == null) {
			detail.setId(IdWorker.getId());
			detail.setStatus(1);
			detail.setCreateBy(WebUtil.getCurrentUser());
			detail.setCreateTime(new Date());
			usingMapper.insertUsingApplyDetail(detail);		
		}else{
			detail.setUpdateTime(new Date());
			detail.setUpdateBy(WebUtil.getCurrentUser());
			usingMapper.updateUsingApplyDetail(detail);
		}	
		
	}

	public int create(UsingApply apply) {
		apply.setId(IdWorker.getId());
		apply.setStatus(1);
		apply.setCreateBy(WebUtil.getCurrentUser());
		apply.setCreateTime(new Date());
		int count =usingMapper.insertUsingApply(apply);	
		if(apply.getDetailList()!=null&&apply.getDetailList().size()>0){
			for(UsingApplyDetail detail:apply.getDetailList()){
				detail.setId(IdWorker.getId());
				detail.setStatus(1);
				detail.setTypeId(Long.parseLong(detail.getTypeVo().getValue()));
				detail.setUsingApplyId(apply.getId());
				detail.setCreateBy(WebUtil.getCurrentUser());
				detail.setCreateTime(new Date());
				usingMapper.insertUsingApplyDetail(detail);		
			}
		}
		return count;
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public UsingApplyDetail qryById(Long id) {
		return usingMapper.qryById(id);
	}

	/**
	 * 分配资产
	 * @param detail
	 */
	public int distribution(UsingApplyDetail detail) {
		//插入更新设备得领用扩张表
		UsingApply apply =usingMapper.qryUsingApply(detail.getId());		
		AssetExtUsing extUse =new AssetExtUsing();
		extUse.setAssetId(detail.getAssetId());
		extUse.setUseBranchId(apply.getBranchId());
		extUse.setEndUser(apply.getUserId());
		extUse.setUpdateBy(WebUtil.getCurrentUser());
		extUse.setUpdateTime(new Date());
		assetExtUsingMapper.updateAssetExtUsing(extUse);
		
		//更新资产表为分配
		ItAsset asset = new ItAsset();
		asset.setId(detail.getAssetId());
		asset.setIsDistribute(true);
		itassetMapper.updateItAsset(asset);
		
		//插入资产生命周期表记录
		AssetRecord re = new AssetRecord();
		re.setId(IdWorker.getId());
		re.setAssetId(detail.getAssetId());
		re.setType(4);
		re.setCreateBy(WebUtil.getCurrentUser());
		re.setCreateTime(new Date());
		assetRecordMapper.create(re);
		 
		//更新申请领用详情为已完成
		detail.setStatus(2);
		detail.setUpdateTime(new Date());
		detail.setUpdateBy(WebUtil.getCurrentUser());
		return usingMapper.distribution(detail);
	}
	

}
